package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.Dto.SetmealDto;
import com.itheima.VO.IdStaVO;
import com.itheima.bean.*;
import com.itheima.common.PageParm;
import com.itheima.dao.CategoryDao;
import com.itheima.dao.SetmealDao;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 根据分类ID查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<Setmeal> findByCategoryId(long id) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, id);
        List<Setmeal> setmeals = setmealDao.selectList(qw);
        return setmeals;
    }

    @Override
    public List<Setmeal> selectByCategoryIdUse(long id) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getStatus,1);
        qw.eq(Setmeal::getCategoryId, id);
        List<Setmeal> setmeals = setmealDao.selectList(qw);
        return setmeals;
    }

    /**
     * 分页查询
     *
     * @param parm
     * @return
     */
    @Override
    public IPage<SetmealDto> selectPage(PageParm parm) {
        IPage<Setmeal> setmealIPage = new Page<>(parm.getPage(), parm.getPageSize());
        IPage<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(parm.getName() != null, Setmeal::getName, parm.getName());
        setmealDao.selectPage(setmealIPage, qw);

        List<SetmealDto> setmealDtoList = setmealIPage.getRecords().stream().map((setmeal -> {
            Category category = categoryDao.selectById(setmeal.getCategoryId());
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(setmeal, setmealDto);
            return setmealDto;

        })).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        setmealDtoPage.setTotal(setmealIPage.getTotal());
        return setmealDtoPage;


    }

    /**
     * 插入套餐所有信息包括菜品
     *
     * @param setmealDto
     * @return
     */
    @Override
    public int insert(SetmealDto setmealDto) {
        int row1 = setmealDao.insert(setmealDto);
        int row2 = 0;
        if (setmealDto.getSetmealDishes() != null && setmealDto.getSetmealDishes().size() != 0) {
            for (SetmealDish dish : setmealDto.getSetmealDishes()) {
                dish.setSetmealId(setmealDto.getId());
                row2 += setmealDishService.insert(dish);
                System.out.println(row2);
            }
            return row2 == setmealDto.getSetmealDishes().size() ? 1 : 0;

        }
        return row1;


    }

    @Override
    public SetmealDto selectById(long id) {
        Setmeal setmeal = setmealDao.selectById(id);
        List<SetmealDish> setmealDishes = setmealDishService.selectBySetmealId(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 更新套餐所有信息
     *
     * @param setmealDto
     * @return
     */
    @Override
    public int updateSetmeal(SetmealDto setmealDto) {
        int row1 = setmealDao.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() != 0) {
            int row2 = 0;
            setmealDishService.deleteDishBySetmealId(setmealDto.getId());
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDto.getId());
                row2 += setmealDishService.insert(setmealDish);
            }
            return row2 == setmealDishes.size() ? 1 : 0;
        }
        return row1;
    }

    /**
     * 批量删除套餐所有信息
     *
     * @return
     */
    public int deleteByIds(List<Long> ids) {
        if (ids != null && ids.size() != 0) {
            for (Long id : ids) {
                Setmeal setmeal = setmealDao.selectById(id);
                if(setmeal.getStatus()==1)
                    return 0;
            }
            int row = 0;
            for (Long id : ids) {
                setmealDishService.deleteDishBySetmealId(id);
                row += setmealDao.deleteById(id);
            }
            if (row == ids.size())
                return 1;
            else return 0;
        }
        return 0;
    }

    @Override
    public int upStatus(IdStaVO idStaVO) {
        List<Long> ids= idStaVO.ids();
        if (ids != null && ids.size() != 0) {
            int row = 0;
            Setmeal setmeal = new Setmeal();
            for (Long id : ids) {
                setmeal.setId(id);
                setmeal.setStatus(idStaVO.getStatus());
                row += setmealDao.updateById(setmeal);

            }
            return row == ids.size() ? 1 : 0;
        } else return 0;
    }


}
